package application

import (
	"github.com/kataras/iris/v12"
	"github.com/kataras/iris/v12/context"
)

func Startup() (app *iris.Application) {
	app=iris.Default()
	app.Use(Recover())
	return
}

func Recover() context.Handler {
	return func(c iris.Context) {
		defer func() {
			if r:=recover();r!=nil{
				c.JSON(iris.Map{
					"success": false,
					"msg":     errorToString(r),
					"data":    nil,
				})
				//终止当前接口
				c.StopExecution()

			}
		}()
		c.Next()
	}

}

// recover错误，转string
func errorToString(r interface{}) string {
	switch v := r.(type) {
	case error:
		return v.Error()
	default:
		return r.(string)
	}
}
